package userregisterAndsign_in;

import java.util.HashMap;
import java.util.Map;

public class UserAuthentication {
    //先要存储 下用户数据
    //哈希表
    public static Map<String,User> users = new HashMap<>();
    static class User{
        private String username;
        private String email;
        private String password;

        public User(String username, String email, String password) {
            this.username = username;
            this.email = email;
            this.password = password;
        }


    }


    public static String authenticate(String username,String password) {
        if (users.containsKey(username) && users.get(username).password.equals(encryptPassword(password))) {
            return "认证成功";
        }else if (!users.containsKey(username)) {
            return "用户名不存在";
        }else {
            return "用户名或密码错误";
        }
    }

    private static String register(String username,String email,String password) {
        //验证 邮箱 格式
        if (!isValidEmail(email)) {
            return "无效邮箱格式";
        }
        //验证密码  的长度
        if (password.length() < 8) {
            return "密码长度不足,至少8位";
        }

        String encryptedPassword = encryptPassword(password);
        users.put(username,new User(username,email,encryptedPassword));
        return "注册成功";
    }


    public static String encryptPassword(String password) {
        //不加密   直接  返回密码
        return password;
    }

    //验证邮箱的
    private static boolean isValidEmail(String email) {
        String reg = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$";
        return email.matches(reg);
    }

    public static void main(String[] args) {
        System.out.println(register("user1", "user1@example.com", "pass123"));
        System.out.println(authenticate("user1", "pass123"));
    }
}
